package com.atguigu.gulimall.gulimallsearch.thread;

import com.atguigu.common.to.SpuBoundsTo;

import java.util.concurrent.*;
import java.util.function.Function;

/**
 * 创建线程的四种方式
 * 1):继承Thread类
 * 2):实现runnable接口
 * 3):实现callable接口+futureTask
 * 4):使用线程池。提交任务
 */

public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws Exception{



        System.out.println("Main...start...");
        long start = System.currentTimeMillis();
//        Future<?> submit = service.submit(new Thread01());
        //使用callable
//        FutureTask<Integer> futureTask = new FutureTask<>(new Thread02());
//        new Thread(futureTask).start();
//        Integer integer = futureTask.get();
//        System.out.println(integer);

        //异步调用的方式
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            return (10 / 2);
//        }, executor).handle((res,thr)->{
//            if (thr!=null){
//                return -1;
//            }else {
//                return res;
//            }
//            });

        //完成后的感知
//        future.whenComplete((res,exc)->{
//            System.out.println("返回的结果："+res);
//            System.out.println("出现的异常："+exc);
//        });


//        Integer integer = future.get();
//        System.out.println("异步执行的结果："+integer);
        //完成后的处理

        //无需感知结果，等待第一个任务完成后直接开启第二个任务
//        CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            return (10 / 2);
//        }, executor).thenRunAsync(()->{
//            System.out.println("第二个异步任务执行。。。");
//        },executor);

        //感知结果
//        CompletableFuture.supplyAsync(()->{
//            return 10/2;
//        },executor).thenAcceptAsync((res)->{
//            System.out.println("任务二感知到结果:"+res);
//        },executor);


        //依赖于上一个任务的结果
//        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
//            return 10 / 2;
//        }, executor).thenApplyAsync((res) -> {
//            return "上一步返回的结果" + res * 2;
//        }, executor);
//
//        String result = completableFuture.get();
//        System.out.println(result);


//        Integer integer = future.get();
//        System.out.println(integer);


        //组合技
//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动了");
//            return 10 / 2;
//        }, executor);


//        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务二启动了");
//            return 10 / 3;
//        }, executor);


        //组合两个结果（等待12两个任务结束以后再执行第三个任务，无法感知前两个运行的结果）
//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务三启动了");
//        },executor);


        //组合两个任务。感知前面运行的结果
//        future01.thenAcceptBothAsync(future02,(res1,res2)->{
//            System.out.println("任务一的结果:"+res1);
//            System.out.println("任务二的结果:"+res2);
//        },executor);

    //组合两个任务。并且拿到最终组合返回后的结果
//        CompletableFuture<Integer> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + f2;
//        }, executor);
//        Integer integer = future.get();
//        System.out.println(integer);


        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品的图片");
            return "hello.jpg";
        }, executor);


        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品的属性");
            return "8+256G";
        }, executor);

        CompletableFuture<String> futurePrice = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品的价格");
            return "7855";
        });

        //组合三个任务
        CompletableFuture<Void> future = CompletableFuture.allOf(futureImg, futureAttr, futurePrice);
        Void aVoid = future.get();
        System.out.println(aVoid);

        long end = System.currentTimeMillis();
        long l = (start - end) / 1000;
        System.out.println("服务运行了"+l+"S");


        System.out.println("Main...end...");

    }


    public static class Thread01 implements Runnable {
        @Override
        public void run() {
            int res = 10 / 6;
            System.out.println(res);
        }
    }


    public static class Thread02 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println(Thread.currentThread().getName() + "开始执行任务。。。");
            int res = 100 % 32;
            System.out.println(Thread.currentThread().getName() + "结束执行任务。。。");
            return res;
        }
    }


}
